<!---

Copyright 2008:

Author: Marco van den Oever
Company: WSO
Website: http://www.webserviceonline.biz
Email: marco@webserviceonline.biz

License: 

This software is licensed under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
For a copy of the GNU General Public License check the license folder or see <http://www.gnu.org/licenses/>.

Description:

wsoFarcryTickets, a Farcry plugin to administer tickets

Build with great support from the Farcry-Dev group! (Google on Farcry-Dev)

Created: 30/11/08

Tested on CF8

--->
<cfsetting enablecfoutputonly="true" />
<cfimport taglib="/farcry/core/tags/formtools" prefix="ft" />
<cfimport taglib="/farcry/core/tags/admin/" prefix="admin" />
<cfimport taglib="/farcry/core/tags/extjs" prefix="extjs" />
<cfimport taglib="/farcry/core/tags/webskin" prefix="skin" />
<!--- get logged in user name --->
<cfset stUser = createObject("component",
application.stcoapi.farUser.packagePath).getByUserID(listfirst(application.security.getCurrentUserID(),"_"))
/>
<!--- if ticket data exists, check for and get new ticket answers from mail server --->

<cfif len(stobj.department)>
  <cfpop action="getALL"
name="mailanswers"
server="#application.config.wsoFarcryTickets.ticketserver#"
port="#application.config.wsoFarcryTickets.ticketservercfpop#"
timeout="#application.config.wsoFarcryTickets.ticketservertim#"
username="#application.config.wsoFarcryTickets.ticketserverus#"
password="#application.config.wsoFarcryTickets.ticketserverpass#">
  <!--- if there are new mail server ticket answers, get them --->
  <cfif mailanswers.recordcount gt 0>
    <cfoutput query="mailanswers">
      <cfset subjectfilter = listlast(mailanswers.subject,"##")>
      <!--- if ticket answer belongs to current ticket, write to answers table --->
      <cfif subjectfilter EQ stobj.objectid>
        <!--- setup cftry and cfcatch block to be sure email is not deleted while not inserted to db --->
        <cftry>
          <cfset obj = createObject
('component',application.stcoapi.ticketanswers.typePath) />
          <cfset stprops = {} />
          <cfset stprops.objectid = CreateUUID() />
          <cfset stprops.ticketobjectid = stobj.objectid />
          <cfset stprops.answer = mailanswers.body />
          <cfset stprops.operator = stUser.userid />
          <cfset stprops.createdby = mailanswers.from />
          <cfset obj.setData(stProperties=stprops) />
          <!--- after inserting to db delete email from mail server --->
          <cfpop action="delete"
server="#application.config.wsoFarcryTickets.ticketserver#"
port="#application.config.wsoFarcryTickets.ticketservercfpop#"
timeout="#application.config.wsoFarcryTickets.ticketservertim#"
username="#application.config.wsoFarcryTickets.ticketserverus#"
password="#application.config.wsoFarcryTickets.ticketserverpass#"
uid="#mailanswers.UID#">
          <cfcatch type="any">
            <h1>Error while inserting email to db, the operation is aborted</h1>
          </cfcatch>
        </cftry>
      </cfif>
    </cfoutput>
  </cfif>
</cfif>
<!--- if a related knowledge base item is requested, cflocate to that item --->
<ft:processForm action="View">
  <cflocation url="/webtop/conjuror/invocation.cfm?objectid=#form.related#&typename=knbase&method=edit&ref=typeadmin&module=customlists/knbase.cfm&plugin=wsoFarcryTickets" addtoken="no">
</ft:processForm>
<ft:processForm action="Save">
  <ft:processFormObjects typename="tickets" bSessionOnly="true" />
  <!--- if a tickets object was succesfully saved --->
  <cfif len(lSavedObjectIDs)>
    <!--- set the tickets object data from session to variable --->
    <cfset stTicket = getData(objectid=lSavedObjectIDs) />
    <cfset obj = createObject
('component',application.stcoapi.tickets.typePath) />
    <cfset stprops = {} />
    <cfset stprops.objectid = stTicket.objectid />
    <cfset stprops.department = stTicket.department />
    <cfset stprops.description = stTicket.description />
    <cfset stprops.priority = stTicket.priority />
    <cfset stprops.status = stTicket.status />
    <cfset stprops.subject = stTicket.subject />
    <cfset obj.setData(stProperties=stprops) />
  </cfif>
  <ft:processformobjects typename="ticketanswers" bSessionOnly="true" />
  <!--- if a ticketanswer object was succesfully saved --->
  <cfif len(lSavedObjectIDs)>
    <!--- set the tickets answer object data from session to variable --->
    <cfset stTicketanswers = getData(objectid=lSavedObjectIDs) />
    <!--- if a ticketanswer was created set the data to db --->
    <cfif len(stTicketanswers.answer)>
      <cfset obj = createObject
('component',application.stcoapi.ticketanswers.typePath) />
      <cfset stprops = {} />
      <cfset stprops.objectid = lSavedObjectIDs />
      <cfset stprops.ticketobjectid = stTicket.objectid />
      <cfset stprops.answer = stTicketanswers.answer />
      <cfset stprops.operator = stUser.userid />
      <cfset obj.setData(stProperties=stprops) />
      <!--- when not already set, set ticket status to "In progress" when a answer is provided by a operator --->
      <cfif stTicket.status neq "inprogress">
        <cfset obj = createObject
('component',application.stcoapi.tickets.typePath) />
        <cfset stprops = {} />
        <cfset stprops.objectid = stTicket.objectid />
        <cfset stprops.status = "inprogress" />
        <cfset obj.setData(stProperties=stprops) />
      </cfif>
    </cfif>
    <!--- get the email address of the ticket starter that wanted to be notified, don't send to operator if started by operator, this page only sends notifications to clients #application.config.wsoFarcryTickets.ticketserver#--->
    <cfif listContains(stTicket.createdby, "@")>
      <cfset etstarteremail = stTicket.createdby>
      <cfelse>
      <cfset etstarteremail = "">
    </cfif>
    <cfif etstarteremail neq "">
      <cfset messageid = listlast(application.config.wsoFarcryTickets.cfmailfrom,"@")>
      <cfmail type="html"
from="#application.config.wsoFarcryTickets.cfmailfrom#"
to="#etstarteremail#"
subject="RE: WSO-Ticket ###stobj.objectid#"
server="#application.config.wsoFarcryTickets.cfmailserver#"
port="#application.config.wsoFarcryTickets.ticketservercfmail#"
>
        RE: WSO-Ticket ###stobj.objectid#<br />
        <br />
        Subject: RE: #stTicket.subject#.<br />
        <br />
        <br />
        Hello #stTicket.createdby#,<br />
        <br />
        #replace(stTicketanswers.answer,Chr(13),"<br>", "all")#<br />
        <br />
        If you have any additional questions/concerns please do not hesitate to reply back.<br />
        <br />
        <br />
        Best regards, #stUser.userid#.<br />
        <br />
        Website: http://www.#messageid#<br />
        Email: #application.config.wsoFarcryTickets.cfmailfrom#
        <cfmailparam name="Message-Id" value="<#createUUID()##messageid#>">
        <cfmailparam name="Reply-To" value="#application.config.wsoFarcryTickets.cfmailfrom#">
        <cfmailparam name="MIME-Version" value="1.0">
      </cfmail>
    </cfif>
  </cfif>
</ft:processForm>
<ft:processForm action="Save,Cancel" exit="true" />
<!--- if ticket data exists then show --->
<cfif len(stobj.department)>
  <!--- if a search query is provided get related items --->
  <cfif isdefined('form.search')>
    <!--- check if related knowledge items exists, if so show --->
    <cfset obj = createObject('component',application.stcoapi.knbase.typePath) />
    <cfset stprops = {} />
    <cfset stprops.whereclause="subject LIKE '%#form.search#%' OR description LIKE '%#form.search#%' OR solution LIKE '%#form.search#%' ORDER by subject asc" />
    <cfset related = obj.getMultipleByQuery(argumentCollection=stprops) />
  </cfif>
  <cfoutput>
    <ft:form>
      <fieldset class="formSection ">
      <legend class="">Search Knowledge Base</legend>
      <div class="fieldSection longchar">
        <label class="fieldsectionlabel "> Search </label>
        <input name="search" type="text" value="#stobj.subject#" />
      </div>
      </fieldset>
      <ft:farcryButtonPanel>
        <ft:farcryButton value="Search" />
      </ft:farcryButtonPanel>
    </ft:form>
    <cfif isdefined('related.recordcount') and related.recordcount gt 0>
      <ft:form>
        <fieldset class="formSection ">
        <legend class="">Related Knowledge Base</legend>
        <div class="fieldSection longchar">
          <label class="fieldsectionlabel "> View </label>
          <select name="related">
            <cfloop query="related">
              <option value="#related.objectid#">#related.subject#</option>
            </cfloop>
          </select>
        </div>
        </fieldset>
        <ft:farcryButtonPanel>
          <ft:farcryButton value="View" />
        </ft:farcryButtonPanel>
      </ft:form>
    </cfif>
  </cfoutput>
</cfif>
<ft:form>
  <ft:object typename="tickets" objectid="#stobj.objectid#" lFields="department,subject,errorurl,description" r_stPrefix="prefix" legend="General Details" />
  <ft:object typename="tickets" objectid="#stobj.objectid#" lFields="priority" r_stPrefix="prefix" legend="Priority" />
  <ft:object typename="tickets" objectid="#stobj.objectid#" lFields="status" r_stPrefix="prefix" legend="Status" />
  <cfif len(stobj.department)>
    <ft:object typename="ticketanswers" lFields="answer" r_stPrefix="prefix" legend="Answer(s)" />
  </cfif>
  <extjs:onReady> <cfoutput> Ext.get("#prefix#lTypenames").on('change', this.onClick, this, {
      buffer: 500,
      fn: function() { 
      renderWorkflowDefWebskins('#stobj.objectid#', Ext.get('#prefix#lTypenames').dom.value);
      }
      });
      
      function renderWorkflowDefWebskins(workflowDefID,lTypenames) {
      
      var el = Ext.get("editWebskins");
      
      if (workflowDefID != '') {
      
      el.slideOut('t', {
      easing: 'easeOut',
      duration: .5,
      remove: false,
      useDisplay: true,
      callback: function() {
      el.load({
      url: "#application.url.webtop#/facade/workflowFacade.cfc?method=renderWorkflowDefWebskins",
      scripts: true,
      autoAbort:true,
      callback: function() {
      el.slideIn('t', {
      easing: 'easeIn',
      duration: .5,
      remove: false,
      useDisplay: true
      })
      },
      params: {
      workflowDefID: workflowDefID,
      lTypenames: lTypenames
      }
      });
      }
      })
      
      
      
      }
      
      } </cfoutput> </extjs:onReady>
  <ft:farcryButtonPanel>
    <ft:farcryButton value="Save" />
    <ft:farcryButton value="Cancel" />
  </ft:farcryButtonPanel>
</ft:form>
<ft:form>
  <!--- get ticket answers --->
  <cfif len(stobj.department)>
    <cfset obj = createObject('component',application.stcoapi.ticketanswers.typePath) />
    <cfset stprops = {} />
    <cfset stprops.whereclause="ticketobjectid = '#stobj.objectid#' ORDER by datetimecreated desc" />
    <cfset ticketanswers = obj.getMultipleByQuery(argumentCollection=stprops) />
    <cfif ticketanswers.recordcount gt 0>
      <cfoutput query="ticketanswers">
        <cfset processtimehour = DateDiff("h", "#stobj.datetimecreated#", "#ticketanswers.datetimecreated#")>
        <fieldset class="formSection ">
        <legend class="">#ticketanswers.datetimecreated# - RE: #stobj.subject#</legend>
        <div class="notes"><a href="/webtop/conjuror/invocation.cfm?objectid=#CreateUUID()#&typename=knbase&method=edit&ref=typeadmin&module=customlists/knbase.cfm&plugin=wsoFarcryTickets&subject=#stobj.subject#&description=#stobj.description#&solution=#ticketanswers.answer#">set to knowledge base</a><br />
          status=#stobj.status#<br />
          start=#stobj.createdby#<br />
          answer=#ticketanswers.createdby#<br />
          passed=#processtimehour# hour</div>
        <div class="fieldSection longchar"> #replace(ticketanswers.answer,Chr(13),"<br>", "all")# </div>
        </fieldset>
      </cfoutput>
    </cfif>
  </cfif>
</ft:form>
<cfsetting enablecfoutputonly="false" />
